// valarray demo
// Created by Administrator on 2025/6/26.
// valarry 用于高效计算的数值数组

#include <iostream>
#include <valarray>
using namespace std;


int main(int argc, char *argv[]) {
    // 拿 v1 中的元素和 v2 下标对应的元素进行运算

    const valarray<int> v1 = {1, 2, 3, 4, 5};
    const valarray<int> v2 = {1, 2, 3, 4, 5};


    const valarray<int> addValue = v1 + v2;
    const valarray<int> subValue = v1 - v2;
    const valarray<int> mulValue = v1 * v2;
    const valarray<int> divValue = v1 / v2;

    cout << "add value: \t";
    for (const auto i: addValue) {
        cout << i << "\t";
    }
    cout << endl;

    cout << "sub value: \t";
    for (const auto i: subValue) {
        cout << i << "\t";
    }
    cout << endl;

    cout << "mul value: \t";
    for (const auto i: mulValue) {
        cout << i << "\t";
    }
    cout << endl;

    cout << "div value: \t";
    for (const auto i: divValue) {
        cout << i << "\t";
    }
    cout << endl;

    /*
     * 其它
     *
     */

    // 获取平方
    const valarray<int> squares = v1 * v1;
    cout << "v1 的平方 value: \t";
    for (const auto i: squares) {
        cout << i << "\t";
    }
    cout << endl;

    // 获取开方（平方根）
    const valarray<int> roots = sqrt(v1);
    cout << "v1 的开方 value: \t";
    for (const auto i: roots) {
        cout << i << "\t";
    }
    cout << endl;
}
